package com.hsnn.medstgmini.settlement.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.settlement.model.SetPayment;
import com.hsnn.medstgmini.settlement.model.form.Payment;
import com.hsnn.medstgmini.settlement.model.form.SetSettlementForm;
import com.hsnn.medstgmini.settlement.service.PaymentManager;
import com.hsnn.medstgmini.settlement.service.SettlementFirstManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.DbUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

@Controller
@RequestMapping("/centerCheckPayment")
public class SetPaymentController {
	private static final Logger log = Logger.getLogger(PaymentController.class);

	@Autowired
	private PaymentManager paymentManager;
	
	@Autowired
	private SettlementFirstManager settlementFirstManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;


	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}
		/**
		 * @category 跳转到中心端审核的支付单页面
		 * @author 韩守松
		 * @date   2015年7月27日
		 * @param  @return
		 */
		@RequestMapping("/toCenterSetPaymentList")
		public String toCenterSetPaymentList(){
			return "/settle/payment/centerSetPaymentList";
		}
		
		@RequestMapping("/getCenterSetPaymentData")
		@ResponseBody
		public Pagination getCenterSetPaymentData(Payment payment){
			log.info("enter getPaymentList method");
			Pagination page = new Pagination(request);
			try {
				DbUtil.jsonToMap(request, page);
				paymentManager.getPaymentList(payment, page);
				page.setSuccess(true);
			} catch (Exception e) {
				log.error("",e);
				page.setSuccess(false);
			}
			return page;
		}
		
		/**
		 * @category 跳转到中心审核的详情列表页面
		 * @author 韩守松
		 * @date   2015年7月28日
		 * @param  @return
		 */
		@RequestMapping("/toCenterCheckSetPaymentDetailList")
		public String toCenterCheckSetPaymentDetailList(Model model,String paymentBillId){
			SetPayment setPayment = paymentManager.getPaymentById(paymentBillId);
//			setPayment.setTotalAmountStr(ParseNumber.toStrDecimal(setPayment.getTotalAmount())); 
//			setPayment.setCreateDatetimeStr(DateUtil.dateTime(setPayment.getCreateDatetime()));
			model.addAttribute("setPayment", setPayment);
			return "/settle/payment/centerCheckSetPaymentDetailList";
		}
		
		@RequestMapping("/getCenterCheckSetPaymentDetailListData")
		@ResponseBody
		public Pagination getCenterCheckSetPaymentDetailListData(Payment payment){
			Pagination page = new Pagination(request);
			try {
				paymentManager.getPaymentDetailList(payment, page);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return page;
		}
		
		/**
		 * @category 跳转到支付明细列表页面
		 * @author 韩守松
		 * @date   2015年7月28日
		 * @param  @return
		 */
		@RequestMapping("/toCenterSetPaymentDetailList")
		public String toCenterSetPaymentDetailList(Model model,String settlementId,String paymentBillId){
			SetSettlementForm SetSettlementForm = settlementFirstManager.getSettlementInfoById(settlementId);
			model.addAttribute("obj", SetSettlementForm);
			model.addAttribute("paymentBillId", paymentBillId);
			model.addAttribute("settlementId2",request.getParameter("settlementId2"));
			return "/settle/payment/centerSetPaymentDetailList";
		}
		
		@RequestMapping("/getCenterSetPaymentDetailListData")
		@ResponseBody
		public Pagination getCenterSetPaymentDetailListData(Payment payment){
			Pagination page = new Pagination(request);
			try {
				paymentManager.getSettlementDetailList(payment, page);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return page;
		}
		
		/**
		 * @category 中心审核支付单
		 * @author 韩守松
		 * @date   2015年7月29日
		 * @param  @return
		 */
		@RequestMapping("/checkPaymentBill")
		@ResponseBody
		public boolean checkPaymentBill(SetPayment setPayment,SysUser user){
			boolean flag = false;
			try {
				user = getUser();
				flag = paymentManager.updatePaymentStatusById(setPayment, user);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return flag;
		}
		
/*				                           支付单支付流程【含核算中心】			    				*/		
		/**
		 * @category 去按配送企业分组的支付单页面【含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:05:04
		 * @param  
		 */
		@RequestMapping("/toPaymentByComp")
		public String toSetCenterList(){
			return "/settle/payment/paymentByComp";
		}
		
		/**
		 * @category 获取按配送企业分组的支付单数据【含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:01:15
		 * @param  
		 */
		@RequestMapping("/getPaymentByCompList")
		@ResponseBody
		public Pagination getCompBySetCenterList(){
			Pagination page = new Pagination(request);
			String compName = request.getParameter("compName");
			if(StringUtils.isNotBlank(compName)){
				page.getConditions().put("compName", compName);
			}
			return 	paymentManager.getCompBySetCenterList(page);
		}
		
		/**
		 * @category 获取按配核算中心分组的支付单数据【含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:01:15
		 * @param  
		 */
		@RequestMapping("/getPaymentBySetCenterList")
		@ResponseBody
		public Pagination getSetCenterList(String compId){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(compId)) {
				page.getConditions().put("compId", compId);
				page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));
				return paymentManager.getPaymentBySetCenterList(page);
			}else{
				return null;
			}
		}
		
		
		/**
		 * @category 去医疗机构分组的支付单明细页面【含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:05:04
		 * @param  
		 */
		@RequestMapping("/toPaymentByHosp")
		public String toPaymentByHosp(String setCenterId,String paymentBillId,String compId){
			request.setAttribute("setCenterId", setCenterId);
			request.setAttribute("paymentBillId", paymentBillId);
			request.setAttribute("compId", compId);
			return "/settle/payment/paymentByHosp";
		}
		
		/**
		 * @category 获取医疗机构分组的支付单明细数据【含核算中心】
		 * @author 应晓川
		 * @date   2015-07-29 13:15:38
		 * @param  
		 */
		@RequestMapping("/getPaymentByHospList")
		@ResponseBody
		public Pagination getPaymentByHospList(){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(request.getParameter("setCenterId")) && StringUtils.isNotBlank(request.getParameter("paymentBillId")) && StringUtils.isNotBlank(request.getParameter("compId")) ) {
				page.getConditions().put("setCenterId", request.getParameter("setCenterId"));
				page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));
				page.getConditions().put("compId", request.getParameter("compId"));
				return paymentManager.getPaymentByHospList(page);
			}else{
				return null;
			}
			
		}
		
		/**
		 * @category 获取医疗机构分组的支付单明细数据【含核算中心】
		 * @author 应晓川
		 * @date   2015-07-29 13:15:38
		 * @param  
		 */
		@RequestMapping("/getDrugDetailByHospList")
		@ResponseBody
		public Pagination getDrugDetailByHospList(){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(request.getParameter("hospId"))) {
				page.getConditions().put("hospId", request.getParameter("hospId"));
				return paymentManager.getPaymentByHospList(page);
			}else{
				return null;
			}
		}
		
		/**
		 * @category 获取药品数据列表【含核算中心】
		 * @author 应晓川
		 * @date   2015年7月29日15:21:51
		 * @param  
		 */
		@RequestMapping("/getSettletdetailinfoByHospList")
		@ResponseBody
		public Pagination getSettletdetailinfoByHospList(){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(request.getParameter("hospId")) && StringUtils.isNotBlank(request.getParameter("setCenterId")) && StringUtils.isNotBlank(request.getParameter("paymentBillId")) && StringUtils.isNotBlank(request.getParameter("compId"))) {
				page.getConditions().put("hospId", request.getParameter("hospId"));
				page.getConditions().put("setCenterId", request.getParameter("setCenterId"));
				page.getConditions().put("paymentBillId", request.getParameter("paymentBillId"));
				page.getConditions().put("compId", request.getParameter("compId"));
				return paymentManager.getSettletdetailinfoByHospList(page);
			}else{
				return null;
			}
		}
		
/*				                           支付单支付流程【不含核算中心】			    				*/		
		
		
		/**
		 * @category 去按配送企业分组的支付单页面【不含核算中心】
		 * @author 应晓川
		 * @date   2015年7月29日16:52:27
		 * @param  
		 */
		@RequestMapping("/toPaymentByNoComp")
		public String toPaymentByNoComp(){
			return "/settle/payment/paymentByNoComp";
		}
		
		/**
		 * @category 获取按配送企业分组的支付单数据【不含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:01:15
		 * @param  
		 */
		@RequestMapping("/getPaymentByNoCompList")
		@ResponseBody
		public Pagination getPaymentByNoCompList(){
			Pagination page = new Pagination(request);
			String compName = request.getParameter("compName");
			if(StringUtils.isNotBlank(compName)){
				page.getConditions().put("compName", compName);
			}
			return 	paymentManager.getPaymentByNoCompList(page);
		}
		
		/**
		 * @category 去按医疗机构分组的支付单页面【不含核算中心】
		 * @author 应晓川
		 * @date   2015年7月28日20:05:04
		 * @param  
		 */
		@RequestMapping("/toHospByNoComp")
		public String toDrugsByHosp(String compId){
			request.setAttribute("compId", compId);
			return "/settle/payment/hospByNoComp";
		}
		
		/**
		 * @category 获取医疗机构分组的支付单明细数据【不含核算中心】
		 * @author 应晓川
		 * @date   2015年7月29日17:41:55
		 * @param  
		 */
		@RequestMapping("/getHospByNoCompList")
		@ResponseBody
		public Pagination getHospByNoCompList(){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(request.getParameter("compId"))) {
				page.getConditions().put("compId", request.getParameter("compId"));
				return paymentManager.getHospByNoCompList(page);
			}else{
				return null;
			}
		}
		
		/**
		 * @category 获取药品明细列表数据【不含核算中心】
		 * @author 应晓川
		 * @date   2015年7月29日18:50:39
		 * @param  
		 */
		@RequestMapping("/getDrugsDetailByNoCompList")
		@ResponseBody
		public Pagination getDrugsDetailByNoCompList(){
			Pagination page = new Pagination(request);
			if (StringUtils.isNotBlank(request.getParameter("hospId"))) {
				page.getConditions().put("hospId", request.getParameter("hospId"));
				return paymentManager.getSettletdetailinfoByHospList(page);
			}else{
				return null;
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		/**
		* @category 跳转支付查看，支付单支付明细
		* @date 2015年7月27日
		* @return
		*/
		@RequestMapping("/toCenterCheckSetmentDetailList")
		public String toCenterCheckSetmentDetailList(Model model, Payment payment) {
			log.info("enter toCenterCheckSetmentDetailList method!");
			model.addAttribute("payment", payment);
			String settlementId = payment.getSettlementId();
			SetSettlementForm settlement = settlementFirstManager
			.getSettlementInfoById(settlementId);
			model.addAttribute("info", settlement);
			return "/settle/centerCheckSetmentDetailList";
		}
		
		@RequestMapping("/getCenterCheckSetmentDetailList")
		@ResponseBody
		public Pagination getCenterCheckSetmentDetailList(String settlementId){
			Pagination page = new Pagination(request);
			page.getConditions().put("settlementId", settlementId);
			page.getConditions().put("hospName", request.getParameter("hospName"));
			try {
				paymentManager.getSetmenDetailList(page);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return page;
		}
		
		
}
